// pages/mine/orderList/orderList.js
import Common from '../../../common/common.js';
let common = new Common;
Page({
  data: {
    pageNumber: 1,
    isEnd: false, //是否到底
    search_key: "",
    pagetype: '0', //0全部 1待发货 2待收货 3已完成
    pageDataList: [],

  },
  onLoad: function (options) {},
  onShow: function () {
    this.getPageDataList(1, 1);
  },
  //查看详情订单
  lookDetails(e) {
    wx.navigateTo({
      url: `/pages/pointList/detail/index?id=${e.currentTarget.dataset.id}`,
    })
  },

  changePagetype(e) {
    this.setData({
      pagetype: e.currentTarget.dataset.type
    })
    this.getPageDataList(1, 1);
  },

  searchKeyOrder(e) {
    console.log(e)
    this.setData({
      search_key: e.detail.value
    })
    this.getPageDataList(1, 1)
  },
  onPullDownRefresh: function () {
    //清空搜索框
    this.setData({
      search_key: '',
      pageNumber: 1,
      isEnd: false
    })
    this.getPageDataList(1, 1)
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },
  onReachBottom: function () {
    if (!this.data.isEnd) {
      let pageNumber = this.data.pageNumber;
      pageNumber += 1;
      this.getPageDataList(pageNumber, 2)
      wx.showLoading({
        title: '正在加载中...',
      })
      this.setData({
        pageNumber
      })
    }
  },
  getPageDataList(page, type) {
    let data = {
      "size": 10,
      "current": page,
      "condition": {
        activityName: this.data.search_key,
        convertStatus: this.data.pagetype, //兑换状态(1、待发货 2、待收货 3、已完成)
        orgSeq: getApp().globalData.orgSeq,
      },
      "ascs": [],
      "descs": []
    }
    common.requestAjax('/api/promotion-service/v1/promotionBuyFree/qryConvertOrder', 'POST', data).then(res => {
      if (res.data.code) {
        let arraylist = this.data.pageDataList || [];
        let isEnd = this.data.isEnd;
        if (type == 1) {
          //下拉刷新
          arraylist = res.data.records;
          isEnd = false;
        } else if (type == 2) {
          //上拉加载
          arraylist = arraylist.concat(res.data.records);
          if (res.data.records.length == 0) {
            //最后一行没数据 到底了
            isEnd = true;
          }
          setTimeout(() => {
            wx.hideLoading()
          }, 500)
        }
        this.setData({
          pageDataList:arraylist,
          isEnd,
        })
      }

    }).catch(err => {
      console.log(err);
    })
  },



})